namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorChaikinOscillator : BaseIndicator
    {
        private string midField = Common.GetGuid();
        private string midMa1Field = Common.GetGuid();
        private string midMa2Field = Common.GetGuid();
        private LineShape tlsChaiKin;
        private LineShape tlsMach;
        private LineShape tlsZero;

        public IndicatorChaikinOscillator()
        {
            base.IndType = IndicatorType.CHAIKIN;
            base.IndName = "CHAIKIN";
            base.IndDesc = "佳庆指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N1", 10.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "N2", 20.0));
            base.IndPeriod.Add(new IndicatorPeriod(2, "M", 6.0));
            base.SourceDic["High"] = "";
            base.SourceDic["Close"] = "";
            base.SourceDic["Low"] = "";
            base.SourceDic["Volume"] = "";
            base.fields = new string[] { "CHAIKIN", "MACH", "ZERO" };
        }

        public override bool Contains(object obj)
        {
            if (((obj != this.tlsChaiKin) && (obj != this.tlsMach)) && (obj != this.tlsZero))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double num = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Volume"]]);
            double num2 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double num3 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["High"]]);
            double num4 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Low"]]);
            double curValue = (num * (((2.0 * num2) - num3) - num4)) / (num3 + num4);
            double num6 = 0.0;
            if (r > 0)
            {
                num6 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][this.midField]);
            }
            curValue += num6;
            double num7 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.midMa1Field, this.midField, curValue, base.Crab.DataSource);
            double num8 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.midMa2Field, this.midField, curValue, base.Crab.DataSource);
            double num9 = num7 - num8;
            double num10 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[2].ParamValue, this.tlsMach.Name, this.tlsChaiKin.Name, num9, base.Crab.DataSource);
            return new double[] { curValue, num7, num8, num9, num10 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsChaiKin, this.tlsMach, this.tlsZero };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.tlsChaiKin);
            base.Crab.DeleteShape(this.tlsMach);
            base.Crab.DeleteShape(this.tlsZero);
            base.Crab.DataSource.Columns.Remove(this.midField);
            base.Crab.DataSource.Columns.Remove(this.midMa1Field);
            base.Crab.DataSource.Columns.Remove(this.midMa2Field);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.midField, numArray[0], r);
                        base.Crab.SetValue(this.midMa1Field, numArray[1], r);
                        base.Crab.SetValue(this.midMa2Field, numArray[2], r);
                        base.Crab.SetValue(this.tlsChaiKin.Name, numArray[3], r);
                        base.Crab.SetValue(this.tlsMach.Name, numArray[4], r);
                        base.Crab.SetValue(this.tlsZero.Name, 0, r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.IndPeriod[2].ParamValue = periods[2];
            this.tlsChaiKin = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.tlsMach = base.Crab.AddLine(fields[1], panelID);
            string name = Common.GetGuid() + "ZERO";
            this.tlsZero = base.Crab.AddLine(name, panelID);
            base.Crab.SetLine(name, "", Color.FromArgb(100, Color.Lime), 1, 0.01, 2, DashStyle.Dash);
            base.SourceDic["High"] = source[0];
            base.SourceDic["Close"] = source[1];
            base.SourceDic["Low"] = source[2];
            base.SourceDic["Volume"] = source[3];
            base.Crab.DataSource.Columns.Add(this.midField);
            base.Crab.DataSource.Columns.Add(this.midMa1Field);
            base.Crab.DataSource.Columns.Add(this.midMa2Field);
        }

        public string MidField
        {
            get
            {
                return this.midField;
            }
            set
            {
                this.midField = value;
            }
        }

        public string MidMa1Field
        {
            get
            {
                return this.midMa1Field;
            }
            set
            {
                this.midMa1Field = value;
            }
        }

        public string MidMa2Field
        {
            get
            {
                return this.midMa2Field;
            }
            set
            {
                this.midMa2Field = value;
            }
        }

        public LineShape TlsChaiKin
        {
            get
            {
                return this.tlsChaiKin;
            }
            set
            {
                this.tlsChaiKin = value;
            }
        }

        public LineShape TlsMach
        {
            get
            {
                return this.tlsMach;
            }
            set
            {
                this.tlsMach = value;
            }
        }

        public LineShape TlsZero
        {
            get
            {
                return this.tlsZero;
            }
            set
            {
                this.tlsZero = value;
            }
        }
    }
}

